import 'package:common/utils/lara_press_native.dart';
import 'package:flutter/foundation.dart';
import 'header.dart';

/// 刷新控制器，自己维护状态
class LaraPressRefreshController extends ChangeNotifier {
  LaraPressRefreshStatus _status = LaraPressRefreshStatus.idle;

  LaraPressRefreshStatus get status => _status;

  /// 设置状态并通知监听者
  void _setStatus(LaraPressRefreshStatus status) {
    if (_status != status) {
      _status = status;
      notifyListeners();
    }
  }

  /// 设置为拉取状态
  void setPulling() => _setStatus(LaraPressRefreshStatus.pulling);

  /// 设置为准备状态
  void setArmed() => _setStatus(LaraPressRefreshStatus.armed);

  /// 设置为刷新状态
  void setRefreshing() {
    _setStatus(LaraPressRefreshStatus.refreshing);
    LaraPressNative.lightImpact();
  }

  /// 设置为完成状态
  void refreshCompleted() {
    _setStatus(LaraPressRefreshStatus.completed);
    LaraPressNative.lightImpact();
  }

  /// 设置为失败状态
  void refreshFailed() => _setStatus(LaraPressRefreshStatus.failed);

  /// 重置为空闲状态
  void reset() => _setStatus(LaraPressRefreshStatus.idle);
}
